package com.youlu.campus.entity;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.annotation.Transient;

/**
 * 证书领取限制
 */
@Getter
@Setter
public class CertificateReceiveLimit {
    /**
     * 电子证书领取所需挑战值
     */
    private Integer caCertificateValue = 0;
    /**
     * 电子证书领取进度文案 如：再获得%d挑战值，可获得电子证书
     */
    private String caCertificateTips = "暂无可领取电子证书";


    /**
     * 纸质证书领取所需挑战值
     */
    private Integer paperCertificateValue = 0;
    /**
     * 纸质证书领取进度文案 如：再获得%d挑战值，可获得纸质证书
     */
    private String paperCertificateTips = "暂无可领取纸质证书";

    /**
     * 纸质证书领取进度文案-最高级提示语 如:恭喜您，已达到领取纸质证书等级！
     */
    private String paperCertificateTopTips = "恭喜您，已达到领取纸质证书等级！";

    /**
     * 达到领取证书进度比率
     */
    @Transient
    private Double rate = 0D;

    /**
     * 达到领取证书进度提示
     */
    @Transient
    private String tips;

    /**
     * 计算达到领取证书进度比率
     *
     * @param rewardValue
     */
    public void setCertificateRate(Integer rewardValue) {
        if (rewardValue == 0 || this.caCertificateValue == 0) {
            this.tips = this.caCertificateTips;
        }
        //挑战值小于领取电子证书条件
        if (rewardValue < caCertificateValue) {
            Integer value = caCertificateValue - rewardValue;
            this.tips = String.format(this.caCertificateTips, value);
            this.rate = rewardValue.doubleValue() / caCertificateValue.doubleValue();
            if (this.rate.intValue() >= 1) {
                this.rate = 1D;
            }
            clear();
            return;
        }
        //挑战值大于领取领取电子证书条件。领取纸质证书
        if (rewardValue >= this.paperCertificateValue) {
            this.tips = this.paperCertificateTopTips;
            this.rate = 1D;
            clear();
            return;
        }
        Integer paperValue = this.paperCertificateValue - rewardValue;
        this.tips = String.format(this.paperCertificateTips, paperValue);
        this.rate = rewardValue.doubleValue() / paperCertificateValue.doubleValue();
        if (this.rate.intValue() >= 1) {
            this.rate = 1D;
        }
        clear();
        return;

    }

    /**
     * 清楚真实数据返回前端用户
     */
    public void clear() {
        this.paperCertificateValue = 0;
        this.caCertificateValue = 0;
        this.caCertificateTips = "";
        this.paperCertificateTips = "";
    }


}
